package com.qiyi.player.base.flv
{
   import flash.utils.IDataInput;
   import flash.utils.IDataOutput;
   import flash.utils.ByteArray;
   
   public class FLVTag extends Object
   {
      
      public function FLVTag(param1:int) {
         super();
         this.bytes = new ByteArray();
         this.bytes.length = TAG_HEADER_BYTE_COUNT;
         this.bytes[0] = param1;
      }
      
      public static const TAG_TYPE_AUDIO:int = 8;
      
      public static const TAG_TYPE_VIDEO:int = 9;
      
      public static const TAG_TYPE_SCRIPTDATAOBJECT:int = 18;
      
      public static const TAG_FLAG_ENCRYPTED:int = 32;
      
      public static const TAG_TYPE_ENCRYPTED_AUDIO:int = TAG_TYPE_AUDIO + TAG_FLAG_ENCRYPTED;
      
      public static const TAG_TYPE_ENCRYPTED_VIDEO:int = TAG_TYPE_VIDEO + TAG_FLAG_ENCRYPTED;
      
      public static const TAG_TYPE_ENCRYPTED_SCRIPTDATAOBJECT:int = TAG_TYPE_SCRIPTDATAOBJECT + TAG_FLAG_ENCRYPTED;
      
      public static const TAG_HEADER_BYTE_COUNT:int = 11;
      
      public static const PREV_TAG_BYTE_COUNT:int = 4;
      
      public function read(param1:IDataInput) : void {
         this.readType(param1);
         this.readRemainingHeader(param1);
         this.readData(param1);
         this.readPrevTag(param1);
      }
      
      public function readType(param1:IDataInput) : void {
         if(param1.bytesAvailable < 1)
         {
            throw new Error("FLVTag.readType() input too short");
         }
         else
         {
            param1.readBytes(this.bytes,0,1);
            return;
         }
      }
      
      public function readRemaining(param1:IDataInput) : void {
         this.readRemainingHeader(param1);
         this.readData(param1);
         this.readPrevTag(param1);
      }
      
      public function readRemainingHeader(param1:IDataInput) : void {
         if(param1.bytesAvailable < 10)
         {
            throw new Error("FLVTag.readHeader() input too short");
         }
         else
         {
            param1.readBytes(this.bytes,1,TAG_HEADER_BYTE_COUNT - 1);
            return;
         }
      }
      
      public function readData(param1:IDataInput) : void {
         if(this.dataSize > 0)
         {
            if(param1.bytesAvailable < this.dataSize)
            {
               throw new Error("FLVTag().readData input shorter than dataSize");
            }
            else
            {
               param1.readBytes(this.bytes,TAG_HEADER_BYTE_COUNT,this.dataSize);
            }
         }
      }
      
      public function readPrevTag(param1:IDataInput) : void {
         if(param1.bytesAvailable < 4)
         {
            throw new Error("FLVTag.readPrevTag() input too short");
         }
         else
         {
            param1.readUnsignedInt();
            return;
         }
      }
      
      public function write(param1:IDataOutput) : void {
         param1.writeBytes(this.bytes,0,TAG_HEADER_BYTE_COUNT + this.dataSize);
         param1.writeUnsignedInt(TAG_HEADER_BYTE_COUNT + this.dataSize);
      }
      
      public function get tagType() : uint {
         return this.bytes[0];
      }
      
      public function set tagType(param1:uint) : void {
         this.bytes[0] = param1;
      }
      
      public function get isEncrpted() : Boolean {
         return this.bytes[0] & TAG_FLAG_ENCRYPTED?true:false;
      }
      
      public function get dataSize() : uint {
         return this.bytes[1] << 16 | this.bytes[2] << 8 | this.bytes[3];
      }
      
      public function set dataSize(param1:uint) : void {
         this.bytes[1] = param1 >> 16 & 255;
         this.bytes[2] = param1 >> 8 & 255;
         this.bytes[3] = param1 & 255;
         this.bytes.length = TAG_HEADER_BYTE_COUNT + param1;
      }
      
      public function get timestamp() : uint {
         return this.bytes[7] << 24 | this.bytes[4] << 16 | this.bytes[5] << 8 | this.bytes[6];
      }
      
      public function set timestamp(param1:uint) : void {
         this.bytes[7] = param1 >> 24 & 255;
         this.bytes[4] = param1 >> 16 & 255;
         this.bytes[5] = param1 >> 8 & 255;
         this.bytes[6] = param1 & 255;
      }
      
      public function get data() : ByteArray {
         var _loc1_:ByteArray = new ByteArray();
         _loc1_.writeBytes(this.bytes,TAG_HEADER_BYTE_COUNT,this.dataSize);
         return _loc1_;
      }
      
      public function set data(param1:ByteArray) : void {
         this.bytes.length = TAG_HEADER_BYTE_COUNT + param1.length;
         this.bytes.position = TAG_HEADER_BYTE_COUNT;
         this.bytes.writeBytes(param1,0,param1.length);
         this.dataSize = param1.length;
      }
      
      public function isEqual(param1:FLVTag) : Boolean {
         if(!(this.tagType == param1.tagType) || !(this.dataSize == param1.dataSize) || !(this.timestamp == param1.timestamp))
         {
            return false;
         }
         var _loc2_:ByteArray = this.data;
         var _loc3_:ByteArray = param1.data;
         if(_loc2_.length != _loc3_.length)
         {
            return false;
         }
         _loc2_.position = 0;
         _loc3_.position = 0;
         while(_loc2_.bytesAvailable)
         {
            if(_loc2_.readByte() != _loc3_.readByte())
            {
               return false;
            }
         }
         return true;
      }
      
      protected var bytes:ByteArray = null;
   }
}
